package com.lm.activiti.boot.cas;

import org.junit.Test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lm_jcf
 * @date 2021/5/14 15:03
 */
public class ReentrantCase {

    private static ReentrantLock lock = new ReentrantLock();


    public static void main(String[] args) {

        new Thread(t1).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("子线程");
                    Thread.sleep(1000L);
                    System.out.println("子线程停止");

                    lock.unlock();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        lock.unlock();
    }

    @Test
    public void test() {

        String s = "-1";

        System.out.println(s.substring(0, s.indexOf("-")));
        System.out.println(s.substring(s.indexOf("-"), s.lastIndexOf("-")));

    }

    private static Runnable t1 = new Runnable() {
        @Override
        public void run() {
            try {

                lock.tryLock();

                System.out.println("获取锁");

                Thread.sleep(6000L);
            } catch (Exception e) {

            }finally {
                lock.unlock();
                System.out.println("t1释放锁");
            }
        }
    };

}
